﻿using System;
using System.Drawing.Imaging;
using System.IO;
using System.Threading;
using System.Net;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DemoPC
{
    public partial class MyDemoPC : Form
    {

        private string sessionId = null;
        private string pcguid = null;
        private MyDemoPCWebClient client = null;

        public MyDemoPC()
        {
            InitializeComponent();
            this.copyBtn.Enabled = false;
            this.stopBtn.Enabled = false;
        }

        private void MyDemoPC_Resize(object sender, System.EventArgs e)
        {
            if (FormWindowState.Minimized == WindowState)
            {
                Hide();
            }
        }

        private void Form1_Resize(object sender, System.EventArgs e)
        {
            if (FormWindowState.Minimized == WindowState)
                Hide();
        }

        private void systemTrayIcon_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            Show();
            WindowState = FormWindowState.Normal;
        }

        private void startBtn_Click(object sender, EventArgs e)
        {
            if (!this.agreeBox.Checked)
            {
                MessageBox.Show(this, "Please read and accept our Disclaimer.");
                return;
            }
            else
            {
                this.startBtn.Enabled = false;
                try
                {
                    this.connectionInfoTxt.BringToFront();
                    if (this.sessionId != null && this.pcguid != null)
                    {
                        client = new MyDemoPCWebClient(this.sessionId, this.pcguid);
                    }
                    else
                    {
                        client = new MyDemoPCWebClient();
                        this.sessionId = client.SessionId;
                        this.pcguid = client.MyPCGuid;

                        this.connectionInfoTxt.Text = "Please use the following infomation to see my desktop:\r\nWeb Site: http://www.mydemopc.com/\r\nScreen Code: " + client.SessionId + "\r\nAccess Pin: " + client.SessionPin;
                        this.copyBtn.Enabled = true;

                    }

                    Thread oThread = new Thread(new ThreadStart(client.postScreen));
                    oThread.Start();
                    this.stopBtn.Enabled = true;

                }
                catch (Exception ex)
                {
                    this.connectionInfoTxt.Text = "";
                    this.agreeBox.BringToFront();
                    this.copyBtn.Enabled = false;
                    this.agreementUrl.BringToFront();
                    this.startBtn.Enabled = true;
                    MessageBox.Show(this, "Error: " + ex.Message);
                }
            }

        }

        private void SessionFinished(object sender, EventArgs e)
        {
            if (MessageBox.Show(this, "Your session finished.", "Session End", MessageBoxButtons.OK) == DialogResult.OK)
            {
                this.Dispose(true);
            }
        }

        private void exitBtn_Click(object sender, EventArgs e)
        {
            this.Dispose(true);
        }

        private void copyBtn_Click(object sender, EventArgs e)
        {
            Clipboard.SetText(this.connectionInfoTxt.Text);
            MessageBox.Show(this, "Access information is in Clipboard now.");
        }

        private void stopBtn_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.stopBtn.Text.Equals("Pause"))
                {
                    this.stopBtn.Enabled = true;
                    this.client.Enabled = false;
                    this.stopBtn.Text = "Resume";
                }
                else
                {
                    this.stopBtn.Enabled = false;
                    this.client.Enabled = true;
                    Thread oThread = new Thread(new ThreadStart(this.client.postScreen));
                    oThread.Start();
                    this.stopBtn.Text = "Pause";
                    this.stopBtn.Enabled = true;
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message);
            }
        }

        private void agreementUrl_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            try
            {
                System.Diagnostics.Process.Start("http://www.mydemopc.com/tos.html");
            }

            catch (Exception exp)
            {
                MessageBox.Show(this, exp.Message);
            }
        }

    }
}
